package Lee_code;

import java.util.HashSet;
import java.util.List;

/**
 * @author blh
 * @date 2024年06月30日 10:39
 */
public class Lee_129_630 {
    HashSet<StringBuilder> set = new HashSet<>();

    public int sumNumbers(TreeNode root) {
        findRoot(root, new StringBuilder());
        int sum=0;
        for (StringBuilder stringBuilder : set) {
            sum = sum + Integer.parseInt(stringBuilder.toString());
        }
        return sum;
    }

    private void findRoot(TreeNode root, StringBuilder sb) {
        if (root.right == null && root.left == null) {
            set.add(new StringBuilder(sb).append(root.val));
            return;
        }
        if (root.left != null) {
            sb.append(root);
            findRoot(root.left, sb);
            sb.deleteCharAt(sb.length()-1);
        }
        if (root.right != null) {
            sb.append(root);
            findRoot(root.right, sb);
            sb.deleteCharAt(sb.length()-1);
        }
    }
}
